$(function () {
    if(localStorage.userName=="null" || localStorage.userName==undefined ||  localStorage.userName==""){
    alert("请先登录");
    window.location.href="login.html";
    }else{
    	$(".yhm").text(localStorage.phone)
     if(window.localStorage["idNum"]!=="null" && window.localStorage["idNum"]!=="" && window.localStorage["idNum"]!==undefined){
    	var weName = $("<p>"+localStorage.realName+"</p>").appendTo(".weName");
    	 var weIdnum = $("<p>"+localStorage.idNum+"</p>").appendTo(".weIdnum");
    	 var weName = $("<p>"+localStorage.email+"</p>").appendTo(".weEmail");
    	 $(".name").remove();
    	 $(".idnum").remove();
    	 $(".email").remove();
    	 $(".weTname").css("display","block");
    	 $(".weTidnum").css("display","block");
    	 $(".weTemail").css("display","block");
    	 $("#updataUInfo").css("display","none");
    	 $(".hqyzm").css("display","none");
         $(".wdsmk").css("display","block");
    	 
     }else{
    	 
     }
    }
    
    
    $("#tcdl").click(function () {
    	window.localStorage.clear();
		window.sessionStorage.clear();
        window.location.href="login.html";
    })
    
    $("#zxzh").click(function(){
    	 window.location.href="zxzh.html"
    })
    	// 发送验证码
	$(".cathCode").click(function() {
		if ( window.localStorage["phone"].length == 11) {
			if ($(".cathCode").hasClass("fsphone")) {
				$(".cathCode").removeClass("fsphone");
				$.ajax({
					url : requestUrl + "app/user/sendPhoneVerifyCode.do",
					type : "POST",
					data : {
						"phone" : window.localStorage["phone"],
						"verifyType" : 5
					},
					dataType : "JSON",
					success : function(json) {
						if (json.code == 0) {
							var i = 0;

							var timer = setInterval(function() {
								$(".cathCode").html(60 - i + "秒后重试");
								if (i == 60) {
									clearInterval(timer);
									$(".cathCode").html("获取验证码");
									$(".cathCode").addClass("fsphone");

								}
								i++;
							}, 1000);
							console.log(json.registerCode);
						} else {
							alert(json.message);
						}
					}
				})
			}
		} else {
			alert("手机号获取失败，请重新登录！");
		}
	})
    $("#updataUInfo").click(function(){
    	if($(".name").val()!==""){
    		if(checkSFZ($(".idnum").val())){
    			if(checkEmail($(".email").val())){
    				if($(".code").val().length==6){
    			        $.ajax({
    						url : requestUrl+"app/user/updateUserInfo.do",
    						type : "POST",
    						data : {
    							"token": sessionStorage.token,
    							"realName":$(".name").val(),
    							"idNum":$(".idnum").val(),
    							"birthday" : $(".idnum").attr("Bri"),
    							"email":$(".email").val(),
    							"verifyCode" : $(".code").val(),
    							"userId":localStorage.userId
    						},
    						dataType : "JSON",
    						success : function(json) {
    							if(json.code == 0){
    							    window.localStorage.email = json.data.email;
    			                    window.localStorage.realName = json.data.realName;
    			                    window.localStorage.idNum = json.data.idNum;
    			                    location.reload(true);
    							}else{
    								alert(json.message);
    							}
    					}	
    		        })
    				}else{
    					alert("验证码有误！")
    				}
    			}else{
    				alert("邮箱有误！")
    			}
    		}else{
    			alert("身份证有误！")
    		}
    	}else{
    		alert("姓名不能为空！")
    	}
    })
})

//检测身份证
function checkSFZ(sfzh) {
	var reg = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/;
	if (reg.test(sfzh)) {
		$(".reg-sfz.reg-input").parent().find(".tishishufu").css("display","none");
		var sfNum = sfzh;
//		if(sfNum.substring(10, 12).substring(0,1)==0){
//			var moth = sfNum.substring(10, 12).substring(1,2);
//		}else{
//			var moth = sfNum.substring(10, 12);
//		}
//		if(sfNum.substring(12, 14).substring(0,1)==0){
//			var day = sfNum.substring(12, 14).substring(1,2);
//		}else{
//			var day = sfNum.substring(12, 14);
//		}
		var birthday= sfNum.substring(6, 10) + "-" + sfNum.substring(10, 12) + "-" + sfNum.substring(12, 14); 
		$(".idnum").attr("Bri",birthday)
		return true;
	} else {
		$(".reg-sfz.reg-input").parent().find(".tishishufu").text("请输入正确的身份证！");
		$(".reg-sfz.reg-input").parent().find(".tishishufu").css("display","initial");
	}
}
//检测邮箱
function checkEmail(email){
	  var reg=/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;    
	    if(reg.test(email)==false){
	    	$(".email.reg-input").parent().find(".tishishufu").text("Email格式不正确，例如web@sohu.com");
			$(".email.reg-input").parent().find(".tishishufu").css("display","initial");
	    return false;
	      }
	    $(".email.reg-input").parent().find(".tishishufu").css("display","none");
	      return true;
	}